<?php
/*
 * Created on Jan 17, 2013
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

function frontend_channel_order(){
    
    //var_dump(function_exists('frontend_channel_order_form_validate'));
	//return 'hello,world';
    return drupal_get_form('frontend_channel_order_form');
}

function frontend_channel_order_init(){
    return request_path();
}

/**
 * @return stdClass
 * - section: string
 * - data: array('options'=>array('global'=>$variables, '[private]'=>$variables))
 *         Private key values:
 *         $privateKey = array(
 *             $region.'.'.$info['view'].'.'.$info['private_key'],
 *             $region.'.'.$info['view'].'.'.$weight,
 *             $region.'.'.$info['view'],
 *             $info['view']
 *         );
 * */
function frontend_channel_order_viewPreset(&$vars, $path){
    
    $re = new stdClass();
    $re->section = 'order';
    $re->data = array();
    
    $re->data['options'] = array();
    
    return $re;
}

function frontend_channel_order_page_build(){
    frontend_attach_clientLibForChannel('order');
}

/**
 * @Description:
 * The parameter [channel or channel/category] should be matched with request_path();
 * The path scan rule is the same with menu path.
 * e.g: "a/b/c" == request_path();
 *      Client library scan order: 
 *      1. "a/b/c"==[channel or channel/category]
 *      2. "a/b"==[channel or channel/category]
 *      3. "a"==[channel or channel/category]
 * The scan process would be break when any one of the client lib settings matched or nothing matched.
 * 
 * HOOK:
 * Be invoke by frontend_attach_clientLibForChannel();
 * */
function frontend_channel_order_clientLibSettings(){
    return array(
        '[channel or channel/category]' => array(
            // the file path is under channel base path "[theme path]/channelLib/sample/[file path][.css]|[.js]"
            'type: css|js' => 'file name prefix', // The real file name rule is "[file name prefix].channel.[js|cssj"  
        ),
        '[channel or channel/category]' => array(
            // the file path is under channel base path "[theme path]/channelLib/sample/[file path][.css]|[.js]"
            'type: css|js' => 'file name prefix', // The real file name rule is "[file name prefix].channel.[js|cssj"  
        )
    );
}

function frontend_channel_order_form($form, &$form_state){
	$form = array();
    
    $form = array(
        '#type'    => 'form',
        '#title' => t('Online Order'),
        '#validate' => array('frontend_channel_form_validate'),
        '#submit'   => array('frontend_channel_form_submit'),
        '#attributes' => array(
            'class' => array('frontend_channel_order_form')
        ),
        '#description' => t('Online order form description...')
    );
    
    $fields = array(
        'company'         => array('type'=>'textfield', 'title'=>t('Full company name'), 'ajaxvalidate'=>'frontend_element_validate_name'),
        'representative'  => array('type'=>'textfield', 'title'=>t('Representative'), 'ajaxvalidate'=>'frontend_element_validate_name'),
        'email'  => array('type'=>'textfield', 'title'=>t('Email'), 'ajaxvalidate'=>'frontend_element_validate_email'),
        'address'  => array('type'=>'textfield', 'title'=>t('Address'), 'ajaxvalidate'=>'frontend_element_validate_address'),
        'telephone'  => array('type'=>'textfield', 'title'=>t('telephone'), 'ajaxvalidate'=>'frontend_element_validate_telephone'),
        'mobile'  => array('type'=>'textfield', 'title'=>t('Mobile'), 'ajaxvalidate'=>'frontend_element_validate_mobile'),
        'fax'  => array('type'=>'textfield', 'title'=>t('Fax'), 'ajaxvalidate'=>'frontend_element_validate_fax'),
        'machine'  => array('type'=>'textarea', 'title'=>t('Machine description'), 'ajaxvalidate'=>'frontend_element_validate_plaintext'),
        'mold'  => array('type'=>'textarea', 'title'=>t('Mold description'), 'ajaxvalidate'=>'frontend_element_validate_plaintext'),
        'special'  => array('type'=>'textarea', 'title'=>t('Special requests'), 'ajaxvalidate'=>'frontend_element_validate_plaintext')
    );
    
    foreach($fields as $name=>$field) {
    	$form[$name] = array(
            '#type'  => $field['type'],
            '#title' => $field['title']
        );
        
        if (!empty($field['ajaxvalidate'])) {
        	$form[$name]['#element_validate'] = array($field['ajaxvalidate']);
            $form[$name]['#limit_validation_errors'] = array(array($name));
            $form[$name]['#ajax'] = array(
                'callback' => 'frontend_channel_form_ajax_element_validate_callback',
                'event'    => 'blur',
                'method'   => 'replace'
            );
        }
    }
    
    $form['send'] = array(
        '#type'  => 'submit',
        '#value' => t('Send order'),
        '#ajax'  => array(
            'callback' => 'frontend_channel_form_ajax_callback',
            'event'    => 'click',
            'method'   => 'replace'
        )
    );
    //debug_record_data(__FUNCTION__, $form);
    return $form;
}
         
function frontend_channel_order_form_validate(&$form, &$form_state){
	$values = $form_state['values'];
    $checkingKey = md5($values['company'] . strtolower($values['email']));
    $queried = db_query('select * from {iknow_orders} where `checkingKey`=:checkingKey', array(':checkingKey'=>$checkingKey));
    if ($queried->rowCount()>0) {
    	form_error($form, t('Your order have been exists in the IknowIndustry.com site, Please waitting feedback.'));
    }
}

function frontend_channel_order_form_submit(&$form, &$form_state){
    $values = $form_state['values'];
    $checkingKey = $values['company'] . $values['email'];
    $fields = array(
        'checkingKey' => md5($values['company'] . strtolower($values['email'])),
        'company' => $values['company'],
        'representative' => $values['representative'],
        'email' => $values['email'],
        'address' => $values['address'],
        'telephone' => $values['telephone'],
        'fax' => $values['fax'],
        'machine' => $values['machine'],
        'mold' => $values['mold'],
        'special' => $values['special'],
        
    );
    
    $inserted = db_insert('iknow_orders')->fields($fields)->execute();
    debug_record_data(__FUNCTION__, $inserted);
}
